package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.domain.po.ErpDepot;
import org.example.domain.po.ErpUser;
import org.example.domain.po.UpdateStatusRequest;
import org.example.service.ErpDepotService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basic/depot")
public class ErpBasicDepotController {

    @Autowired
    private ErpDepotService erpDepotService;

    /**
     * 分页条件查询
     * @param current
     * @param size
     * @param name
     * @param remark
     * @return
     */
    @GetMapping("/findPage")
    public Result<Page<ErpDepot>> findByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size,
            @RequestAttribute("user") ErpUser user,
            String name,
            String remark
    ){
        Page<ErpDepot> page = new Page<>(current, size);
        page = erpDepotService.findByPage(page, name, remark, user.getTenantId());
        return Result.success(page);
    }

    /**
     * 新增
     * @param user
     * @param erpDepot
     * @return
     */
    @PostMapping("/insert")
    public Result<String> insert(@RequestAttribute("user") ErpUser user, @RequestBody ErpDepot erpDepot){
        erpDepot.setEnabled(true);
        erpDepot.setIsDefault(false);
        erpDepot.setTenantId(user.getTenantId());
        erpDepot.setDeleteFlag("0");
        return erpDepotService.save(erpDepot) ? Result.success("新增成功！") : Result.error("新增失败！");
    }

    /**
     * 修改
     * @param erpDepot
     * @return
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody ErpDepot erpDepot){
        return erpDepotService.updateById(erpDepot) ? Result.success("编辑成功！") : Result.error("编辑失败！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<String> remove(@PathVariable("id") Integer id){
        erpDepotService.removeById(id);
        return Result.success("删除成功！");
    }

    /**
     * 批量删除
     * @param erpDepots
     * @return
     */
    @PostMapping("/batch")
    public Result<String> removeAll(@RequestBody List<ErpDepot> erpDepots){
        return erpDepotService.removeBatchByIds(erpDepots) ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    /**
     * 批量修改
     * @param modify
     * @return
     */
    @PutMapping("/modifyTheState")
    public Result<String> modifyTheState(@RequestBody UpdateStatusRequest modify){
        boolean theState = erpDepotService.modifyTheState(modify.getIds(), modify.getStatus());
        return theState ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    /**
     * 设置默认
     * @param id
     * @return
     */
    @PutMapping("/SetDefault/{id}")
    public Result<String> SetDefault(@PathVariable("id") Integer id){
        boolean b = erpDepotService.SetDefault(id);
        return b ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    /**
     * 查询启用数据
     * @return
     */
    @GetMapping("/findByEnabled")
    public Result<List<ErpDepot>> findByEnabled(@RequestAttribute("user") ErpUser user) {
        QueryWrapper<ErpDepot> wrapper = new QueryWrapper<>();
        wrapper.eq("enabled",1);
        wrapper.eq("delete_Flag",0);
        wrapper.eq("tenant_id",user.getTenantId());
        List<ErpDepot> list = erpDepotService.list(wrapper);
        return Result.success(list);
    }
}
